package cn.edu.xmu.orderpayment.payment.Service;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.payment.Dao.ErrorAccountDao;
import cn.edu.xmu.orderpayment.payment.model.bo.ErrorAccount;
import cn.edu.xmu.orderpayment.payment.model.bo.Payment;
import cn.edu.xmu.orderpayment.payment.model.bo.Refund;
import cn.edu.xmu.orderpayment.payment.model.vo.ModificationBody;
import cn.edu.xmu.orderpayment.payment.model.vo.SimpleErrorAccount;
import cn.edu.xmu.orderpayment.payment.model.vo.SimplePayment;
import cn.edu.xmu.orderpayment.payment.model.vo.SimpleRefund;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class ErrorAccountService {

    @Autowired
    private ErrorAccountDao errorAccountDao;

    /**
     * 平台管理员查询错账信息
     *
     * @param documentId
     * @param state
     * @param beginTime
     * @param endTime
     * @param page
     * @param pageSize
     * @return
     * @Authou Zhang Liu
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject adminSelecterrorAccounts(Long patternId, String documentId, Byte state, LocalDateTime beginTime, LocalDateTime endTime, Integer page, Integer pageSize) {
        return new ReturnObject(errorAccountDao.adminSelecterrorAccounts(patternId, documentId, state, beginTime, endTime, page, pageSize));
    }

    /**
     * 平台管理员查询错账信息详情
     *
     * @param id
     * @return
     * @Author Zhang Liu
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject adminSelectCertainerrorAccounts(Long id) {
        return new ReturnObject(errorAccountDao.selecterrorAccountsByPrimaryKey(id));
    }

    /**
     * 平台管理员修改错账信息详情
     *
     * @param id
     * @return
     * @Author Zhang Liu
     */
    @Transactional(rollbackFor = Exception.class)
    public ReturnObject adminUpdateCertainErrorAccounts(Long id, ModificationBody modificationBody, Long loginUser, String loginUserName) {
        ErrorAccount bo = new ErrorAccount();
        //校验无误就为bo赋值
        if (modificationBody.getState() != null){
            if(!modificationBody.getState().equals(ErrorAccount.State.TOBEPAID.getCode()))
                return new ReturnObject(ReturnNo.STATENOTALLOW,"当前状态禁止此操作");// 在业务逻辑层校验该支付单的状态
            bo.setState(modificationBody.getState());
        }
        if (modificationBody.getDescr() != null){
            bo.setDescr(modificationBody.getDescr());
        }
        bo.setId(id);
        return new ReturnObject(errorAccountDao.updateErrorAccount(bo, loginUser, loginUserName));
    }

    /**
     * 插入错账记录
     *
     * @param payment
     * @param refund
     * @return
     * @Author Zhang Liu
     */
//    @Transactional(rollbackFor = Exception.class)
//    public ReturnObject insertErrorAccount(SimplePayment payment, SimpleRefund refund, ErrorAccount errorAccount, Long loginUser, String loginUserName) {
//        ReturnObject ret = new ReturnObject();
//        if (payment != null) {
//            errorAccount.setTime(payment.getPayTime());
//            errorAccount.setDocumentId(payment.getDocumentId());
//            errorAccount.setDescr(payment.getDescr());
//            ret = errorAccountDao.insertErrorAccount(errorAccount, loginUser, loginUserName);
//        }
//        if (refund != null) {
//            errorAccount.setTime(refund.getRefundTime());
//            errorAccount.setDocumentId(refund.getDocumentId());
//            ret = errorAccountDao.insertErrorAccount(errorAccount, loginUser, loginUserName);
//        }
//        return Common.getRetVo(ret, SimpleErrorAccount.class);
//    }
}
